package com.cian.controller;


import com.cian.entity.CommodityInform;
import com.cian.entity.Properties;
import com.cian.entity.dto.CommodityDTO;
import com.cian.entity.dto.CommodityForStockAddDTO;
import com.cian.service.impl.CommodityInformServiceImpl;
import com.cian.service.impl.StockServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 商品详细信息表 前端控制器
 * </p>
 *
 * @author Cian
 * @since 2021-03-07
 */
@RestController
@RequestMapping("//commodity")
public class CommodityInformController {
    @Autowired
    Properties properties;
    @Autowired
    CommodityInformServiceImpl commodityInformService;
    @Autowired
    StockServiceImpl stockService;

    @RequestMapping("/add")
    public Boolean commodityAdd(@RequestParam("name") String name, @RequestParam("file")MultipartFile file){
        String fileName = file.getOriginalFilename();	 // 图片名字
        UUID uuid=UUID.randomUUID();
        String newfileName = uuid.toString().substring(0,16) + fileName.substring(fileName.indexOf('.')); //文件重命名
//        //文件存放路径
        String Path = properties.getPhotoDir() + newfileName;
        // 文件处理
        File dest = new File(Path);
        try{
            // 检验目录是否存在
            if(!dest.getParentFile().exists()){
                boolean isMkdir = dest.getParentFile().mkdirs();
                if(!isMkdir){
                    System.out.println("未能成功的创建父文件夹");
                    return false;
                }
            }
            file.transferTo(dest);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
//        System.out.println(Path);
        // 持久化到数据库
        CommodityInform commodityInform = new CommodityInform();
        commodityInform.setCoName(name);
        commodityInform.setCoPath(Path);
        commodityInform.setCoFileName(newfileName);
        commodityInformService.insert(commodityInform);
        return true;
    }

    @RequestMapping("/obtain")
    public List<CommodityDTO> showData(@RequestParam("next-page") int nextPage){
        // 前端的nextPage从1开始，后台需要进行处理
        nextPage = nextPage <= 0 ? 0: nextPage - 1;
        return commodityInformService.selectAll(nextPage);
    }

    @RequestMapping("/obtain/{select}")
    public List<CommodityDTO> showDataByCondition(@PathVariable("select") String select,
                                                  @RequestParam("condition") String condition,
                                                  @RequestParam("next-page") int nextPage){
        // 前端的nextPage从1开始，后台需要进行处理
        nextPage = nextPage <= 0 ? 0: nextPage - 1;
        if(select.equals("1")){
            return commodityInformService.selectByCondition(select, condition, nextPage);
        }else if(select.equals("2")){
            return commodityInformService.selectByCondition(select, condition, nextPage);
        }
        return null;
    }

    @RequestMapping("/delete")
    public Boolean deleteRow(@RequestParam("id") int id){
        // 删掉了商品，默认将库存中对应的商品全部删除
        stockService.deleteStockByCommodityCode(id);
        return commodityInformService.delete(id);
    }

}

